var vehicle;
var thisF;
var vehicleproxy;
var btn_tow_status;
var currentid,currentemail,currentname;
var currentnotes=new Array();
var ListPageMediator = Objs("view.ListPageMediator",
	Mediator,
{
	
	initialize: function( name, viewComponent )
	{ 
		ListPageMediator.$super.initialize.call( this, name, viewComponent );
        vehicleproxy = this.facade.retrieveProxy( ProxyNames.VEHICLE_PROXY );
		thisF=this;
		this.registerListeners();
        this.intializeMap();
		
	},
        
    intializeMap:function()
	{
		var mapdivMap = document.getElementById("map-content");
		mapdivMap.style.width = '74%';
		mapdivMap.style.height = '100%';
		center = new google.maps.LatLng(37.423021 , -122.083739);
		myOptions = {
				zoom: 6,
				center:center,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
		listmap=new google.maps.Map(document.getElementById("map-content"), myOptions);
	},
        
	registerListeners: function()
	{
		var listPage = this.get();    
		listPage.addEventListener( ListPage.NEXT, this.onNext, this );
		listPage.addEventListener( ListPage.PREVIOUS, this.onPrevious, this );
              
	},
	get: function()
	{
		return this.viewComponent;
	},
	unregisterListeners: function()
	{
	},
	listNotificationInterests: function()
	{
		return [
            NotificationNames.GETCOUNT_READY,
			NotificationNames.INTIAL_VLIST,
			 NotificationNames.CLEAR_LIST,
			NotificationNames.LOAD_SUCCESS_1,
			NotificationNames.CHANGE_STATUS,
			NotificationNames.DELETE,
NotificationNames.SAVEREM,		
		];
	},
    handleNotification: function( note )
	{ 
		
		switch ( note.getName() )
		{ 
                        case NotificationNames.GETCOUNT_READY:
				this.setCount();
				break;
                                
			case NotificationNames.INTIAL_VLIST:
		        this.displayList();
				break;
				case NotificationNames.SAVEREM:
		      vehicleproxy.getVehicleByPage(1);
				break;
			case NotificationNames.CHANGE_STATUS:
					this.statuschangesuc(note.getBody());
				break;	
			case NotificationNames.DELETE:
					this.delsuc(note.getBody());
				break;
			case NotificationNames.CLEAR_LIST:
		        document.getElementById("vehicle-list-component").innerHTML = '<div class="default-loader"><img width="100" height="100" src="images/loading.gif"/></div>';
				break;
					case NotificationNames.LOAD_SUCCESS_1:
				$("#c").append(note.getBody());
				break;
				
		}
	},
	statuschangesuc:function(stname)
	{
		 vehicleproxy.getVehicleByPage(1);
	  $("#statusadminchange").show();
	   $("#statusadminchange").html("Status Changed to" + stname);
	   setTimeout(function() {
    $("#statusadminchange").hide();
		}, 5000);
	},
	delsuc:function(stname)
	{
		 vehicleproxy.getVehicleByPage(1);
		$("#statusadminchange").show();
		$("#statusadminchange").html("Vehicle Deleted");
		setTimeout(function() {
		$("#statusadminchange").hide();
			}, 5000);
	},
	displayList:function()
	{   
	
		 $("#pagePanel").show();
		 $(".container").show();
         $("#progresslistpage").hide();   
		var vehicle = vehicleproxy.getVehicleTable();
		var vCount = vehicleproxy.getVehicleCount();
		var limit = vehicleproxy.getLimit();
               
		if(vCount == 0){
			vCount = 1;
		}		
		 var division = Math.floor(vCount/limit);
         var mod = vCount%limit;
         if(mod > 0){
            division++;
         }
		
		 $("#pagination").paginate({
                     
			count 		: division,
			start 		: currentPage,
			display     : 10,
			border					: false,
			text_color  			: '#888',
			background_color    	: '#EEE',	
			text_hover_color  		: 'black',
			background_hover_color	: '#CFCFCF',
			images					: false,
			mouse					: 'press',
			onChange     			: function(page){   
										vehicleproxy.getVehicleByPage(page);
									  }
		 });
                
		 var division = myObject.count/limit;
         var mod = myObject.count%limit;
         if(mod == 0)
         {
            pages=division;
         }
         else
         {
		   var temp=Math.floor(division);
           pages=temp+1;
         }
		 
		document.getElementById("page-status-count").innerHTML = vehicleproxy.getPageStatus();        
        var  innerHtmlCode='';
		 innerHtmlCode = "<table id='vehlist-table' class='table table-outer-bordered' style=font-size:12px;>";
		 innerHtmlCode+="<tbody>";
        if(global_user.admin_type=="Tower Administrator")
                     innerHtmlCode +="<tr style='background-color:#F5F5F5;'><td></td><td><a href='javascript:void();'><u>Vehicle Info</u></a></td><td><a href='javascript:void();'><u>Seller</u></a></td><td><a href='javascript:void();'><u>Location</u></a></td><td><a href='javascript:void();'><u>Preferred Pick Up</u></a></td><td><a href='javascript:void();'><u>Buyer</u></a></td><td><a href='javascript:void();'><u>Status</u></a></td><td></td></tr>";
		for(i=0;i<vehicle.length;i++){		
			if(global_user.admin_type=="Tower Administrator")
			   {	
                               
				   if(vehicle[i].tow_is_read == true)
						innerHtmlCode += "<tr id='vehicle-list' style='background-color:#F5F5F5;'>";
				   else
					   innerHtmlCode += "<tr id='vehicle-list' class='info'>";
			   }   
                else
                {    
					if(vehicle[i].is_read == true){ 
						 innerHtmlCode += "<tr id='vehicle-list' style='background-color:#F5F5F5;'>";
					}else{
						innerHtmlCode += "<tr id='vehicle-list' class='info'>";
					}
                }
                                var label ='';
				var  street_address;
				if(vehicle[i].v_street_address==''&&vehicle[i].v_city!=''&&vehicle[i].v_state_name_name!='')
					 street_address='1323';
				else
					street_address = vehicle[i].v_street_address;
				var datestring=vehicle[i].update_date;
				var year = datestring.substr(0, 4);
				var month =datestring.substr(5, 2);
				var date = datestring.substr(8, 2);
				var time = datestring.substr(11, 8);
				var curdate=month+'-'+date+'-'+year+' '+time;	
                             
				if(global_user.admin_type=="Tower Administrator")
				{ 
                                        
					innerHtmlCode +="<td style='width:30px;'><img  style=margin-top:5px; src='" + vehicle[i].imageurl + "' width='30' height='30'/></td><td width='200px;'><a href='javascript:thisF.showInfoPage("+ vehicle[i].vehicle_id +", "+ vehicle[i].vehicle_role +");'>" + vehicle[i].year + " " + vehicle[i].make_name + " " + vehicle[i].model_name + "</a><div class='muted'></i><small><b>#"+ vehicle[i].vehicle_id + "</b></small></div></td><td>" + vehicle[i].first_name + "<div class='muted'><small>" + vehicle[i].phone + "</small></div></td><td width:100px;>" + vehicle[i].v_city + ((vehicle[i].v_state_name>0)?", "+vehicle[i].v_state_name:'') +"<div class='muted'><small>" + vehicle[i].v_street_address + "</small></div></td><td>" + vehicle[i].remove_by + "</td><td>" + vehicle[i].assist_by + "</td>";
					if(currentStatus=='All')
					{
						if(vehicle[i].tow_status_name=="Delivered")
                                                        label = 'label-success';
						else if(vehicle[i].tow_status_name=="Scheduled")
                                                        label = 'label-info';
						else if(vehicle[i].tow_status_name=="New vehicle")
                                                         label = 'label-warning';
                                                else
                                                         label='';
							
					}
                    innerHtmlCode +="<td style='vertical-align:middle'><div class='dropdown'><a class='dropdown-toggle' id='tow_status_list_"+ vehicle[i].vehicle_id +"' data-toggle='dropdown' href='#'><span class='label "+ label +"'>"+vehicle[i].tow_status_name+" <b class='caret'></b></span></a><ul class='dropdown-menu pull-left'><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",1);' id='tow_status_list_new_"+ vehicle[i].vehicle_id +"'><a  href='javascript:void();'><i class='icon-inbox'></i>&nbsp&nbspNew</a></li><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",2);' onclick='thisF.setTowStatus();' id='tow_status_list_scheduled_"+ vehicle[i].vehicle_id +"'><a  href='javascript:void();'><i class='icon-calendar'></i>&nbsp&nbspScheduled</a></li><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",5);' id='tow_status_list_pickedup_"+ vehicle[i].vehicle_id +"'><a href='javascript:void();'><i class='icon-shopping-cart'></i>&nbsp&nbspPicked Up</a></li><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",3);' id='tow_status_list_delivered_"+ vehicle[i].vehicle_id +"'><a href='javascript:void();'><i class='icon-check'></i>&nbsp&nbspDelivered</a></li><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",6);' id='tow_status_list_unabletocontact_"+ vehicle[i].vehicle_id +"'><a href='javascript:void();'><i class='icon-eye-close'></i>&nbsp&nbspUnable to Contact</a></li><li onclick='thisF.setTowStatus("+ vehicle[i].vehicle_id +",7);' id='tow_status_list_cancelled_"+ vehicle[i].vehicle_id +"'><a href='javascript:void();'><i class='icon-remove'></i>&nbsp&nbspCancelled</a></li></ul></div></td>";
                     innerHtmlCode += "<td width='50px'><div class='btn-group'><a style='margin-top:8px;' class='btn btn-mini dropdown-toggle' data-toggle='dropdown' href='#'><i class='icon-cog'></i><span class='caret'></span></a><ul class='dropdown-menu pull-right'><li><a target='blank' href='javascript:void();'><i class='icon-envelope'></i>&nbsp&nbspAdd Message</a></li><li><a target='blank' href='javascript:void();'><i class='icon-print'></i>&nbsp&nbspPrint Tow Ticket</a></li><li><a href='javascript:void();'><i class='icon-list-alt'></i>&nbsp&nbspCreate Receipt</a></li></ul></div></td></tr>";
				}   
                if(global_user.admin_type!="Tower Administrator")
				{    
                                    
                                   
                                          
					innerHtmlCode +="<td width=35px;><img style=margin-top:5px; src='" + vehicle[i].imageurl + "' width='30' height='30'/></td><td  width=48px;><a href='#' >";
					var unread_count=0;
					for(j=0;j<vehicle[i].messages.length;j++)
					{
						if(vehicle[i].messages[j].isRead==false)
						{ 
							unread_count++;
							}
					}
					innerHtmlCode +=unread_count+"<i class='icon-envelope'></i>";
										
						innerHtmlCode +="</a><br/><a class='popover-right' id='mapicon-"+i+"'  style='cursor:pointer' rel='popover' data-original-title='<h5>"+vehicle[i].year+" "+vehicle[i].make_name+" "+vehicle[i].model_name+"<div class=muted><small style=font-size:11px;>"+vehicle[i].v_street_address+","+vehicle[i].v_city+" "+vehicle[i].v_state_name+"</small></div></h5>'  data-content='<img src=http://maps.googleapis.com/maps/api/staticmap?sensor=true&size=250x170&zoom=13&maptype=roadmap&markers=color:green%7Clabel:A%7C"+street_address.replace(/ /g,"+")+","+vehicle[i].v_city+","+vehicle[i].v_state_name+"&sensor=false />'><i style=margin-left:6px;  class='icon-map-marker'></i></a></td><td width=230px;><a href='javascript:thisF.showInfoPage("+ vehicle[i].vehicle_id +", "+ vehicle[i].vehicle_role +");'>" + vehicle[i].year + " " + vehicle[i].make_name + " " + vehicle[i].model_name + "</a><div class='muted'></i><small>#"+ vehicle[i].vehicle_id + "</small></div></td><td width=150px;>" + vehicle[i].first_name + "<div class='muted'><small>" + vehicle[i].e_mail + "</small></div></td><td width=140px;>" + vehicle[i].v_state_name +"/"+ vehicle[i].v_city + "<div class='muted'><small>" + ((vehicle[i].v_state_name>0)?vehicle[i].v_state_name:'') + "</small></div></td>";
					if(currentStatus=='All')
					{
						if(vehicle[i].tow_status_name=="Delivered")
							innerHtmlCode +="<td ><span class='label label-success'>"+vehicle[i].tow_status_name+"</span></td>";
						else if(vehicle[i].tow_status_name=="Inprogress")
							innerHtmlCode +="<td ><span class='label label-info'>"+vehicle[i].tow_status_name+"</span></td>";
						else if(vehicle[i].tow_status_name=="New vehicle")
							innerHtmlCode +="<td ><span class='label label-warning'>"+vehicle[i].tow_status_name+"</span></td>";
					}
					innerHtmlCode +="<td width=210px;>" + curdate + "<div class='muted'><small>" + vehicle[i].assist_by + "</small></div></td>";
					innerHtmlCode +="<td width=120px;>$" + vehicle[i].asking_price + "</td><td width=160px;>" + vehicle[i].current_action + "<br/>";
					if(vehicle[i].isReminder == true){ 
							 innerHtmlCode += "<span class='label label-important'>Reminder</span>";
					}else if(vehicle[i].is_watch == true){
							 innerHtmlCode += "<span class='label label-success'>Flagged</span>";
					}else if(vehicle[i].is_message == true){
							 innerHtmlCode += "<img style='margin-right:10px' src='images/message.png' width='24' height='24'/>";
					}
					innerHtmlCode += "<td width='35px'><div class='btn-group'><a class='btn btn-mini dropdown-toggle' data-toggle='dropdown' href='#'><i class='icon-cog'></i><span class='caret'></span></a><ul class='dropdown-menu '><li onclick='thisF.registerwatchlist("+i+");' ><a href='' data-toggle='modal' data-target='#watchlist-modal'><i class='icon-eye-open'></i>&nbsp&nbspAdd Watchlist</a></li><li><li onclick='thisF.registermessage("+i+");'><a href='#' data-toggle='modal'  data-target='#message-modal'><i class='icon-envelope'></i>&nbsp&nbspAdd Message</a></li><li onclick='thisF.registershare("+i+");'><a href='#' data-toggle='modal'  data-target='#share-modal'><i class='icon-share'></i>&nbsp&nbspShare</a><li><a href='print-page.php?id="+ vehicle[i].vehicle_id +"' target='blank'><i class='icon-print'></i>&nbsp&nbspPrint</a></li><li><a target='blank' href='receipt.php?id="+vehicle[i].vehicle_id+"'><i class='icon-list-alt'></i>&nbsp&nbspCreate Receipt</a></li><li onclick='thisF.registerprivatenote("+i+");'><a href='#' data-toggle='modal'  data-target='#privatenote-modal'><i class='icon-edit'></i>&nbsp&nbspPrivate Note</a></li>";
					innerHtmlCode +="<li class='dropdown-submenu pull-left'><a href=#><i class='icon-wrench'></i>&nbsp&nbspChange Status</a><ul class='dropdown-menu pull-left'><li>";
					if(vehicle[i].status=="New Submission")
						innerHtmlCode +="<a href=#>&#10003;New Submission</a>";
					else
						innerHtmlCode +="<a href=# onclick='thisF.statuschange("+ vehicle[i].vehicle_id +",1);'> New Submission</a>";
					if(vehicle[i].status=="Bid Accepted")
						innerHtmlCode +="<a href=#>&#10003;Bid Accepted</a>";
					else
						innerHtmlCode +="<a href=# onclick='thisF.statuschange("+ vehicle[i].vehicle_id +",2);'> Bid Accepted</a>";
					if(vehicle[i].status=="Now In Possession")
						innerHtmlCode +="<a href=#>&#10003;Now In Possession</a>";
					else
						innerHtmlCode +="<a href=# onclick='thisF.statuschange("+ vehicle[i].vehicle_id +",3);'> Now In Possession</a>";	
					if(vehicle[i].status=="All Vehicle")
						innerHtmlCode +="<a href=#>&#10003;All Vehicle</a>";
					else
						innerHtmlCode +="<a href=# onclick='thisF.statuschange("+ vehicle[i].vehicle_id +",4);'> All Vehicle</a>";	
					innerHtmlCode +="</li></ul><li><a href='javascript:thisF.showInfoPage("+ vehicle[i].vehicle_id +", "+ vehicle[i].vehicle_role +");'><i class='icon-pencil'></i>&nbsp&nbspEdit</a></li><li><a <a href=# onclick='thisF.deletevehicle("+ vehicle[i].vehicle_id +");'><i class='icon-trash'></i>&nbsp&nbspDelete</a></li></ul></div></td></tr>";
					
				} 
				
					
				   
		
        }
                
		innerHtmlCode += '</tbody></table>';
		$("#vehicle-list-component").html(innerHtmlCode);
		 $("a[class=popover-right]").popover();
		 $("a[class=popover-left]").popover({placement:'left'});
		 $("a[rel=popover]").click(function(){return false;});
		 $(document).click(function() {$("a[rel=popover]").popover('hide');});
		   if(pages==1)
			{
				$("#pagePanel").hide();
			}     
  
		this.sendNotification( NotificationNames.SHOW_MAP_VIEW, 'map' );
       
	},
	deletevehicle:function(vid)
	{
		var cstatus="Deleted"; 
		bootbox.confirm("Are you want to Delete Vehicle?", "Cancel", "Ok", function(result) {
			 if(result==true)
			 {
			 	vehicleproxy.setStatus_list(vid,cstatus);
			 }
			 
			 });
	},
	statuschange:function(vid,statusvalue)
	{
		var cstatus;	
        if(statusvalue==1)
			cstatus="New Submission"; 
		if(statusvalue==2)
			cstatus="Bid Accepted"; 
		if(statusvalue==3)
			cstatus="Now In possession"; 
		if(statusvalue==4)
			cstatus="All Vehicle";
		if(statusvalue==5)
			cstatus="Deleted";	
        bootbox.confirm("Are  you want to change the status?", "Cancel", "Ok", function(result) {
			 if(result==true)
			 {
			
			 	vehicleproxy.setStatus_list(vid,cstatus);
			 }
			 });
	},
    registermessage:function(index)
	{
	    
		currentid=vehicle[index].vehicle_id;
		currentemail=vehicle[index].e_mail;
		currentname=vehicle[index].first_name;
			vehicleproxy.getMessages();
			
		if(this.facade.hasMediator(MediatorNames.MESSAGE_POP_MEDIATOR)==false)
		{								
			var messagepopPage = new MessagepopPage();   
			var messagepopMediator = new MessagepopMediator( MediatorNames.MESSAGE_POP_MEDIATOR, messagepopPage);
			this.facade.registerMediator( messagepopMediator );
		}
	} ,    
	registerwatchlist:function(index)
	{
	    
		currentid=vehicle[index].vehicle_id;
		currentemail=vehicle[index].e_mail;
		currentname=vehicle[index].first_name;
		if ($(jQuery("#checkbox-1")).attr('checked')) {
			$('#hidecont').show('slow');
		}
		else
		{
			$('#hidecont').hide('slow');
		}
		
		if(this.facade.hasMediator(MediatorNames.WATCH_MEDIATOR)==false)
		{
		var watchPage = new WatchPage();
             
		var watchPageMediator = new WatchPageMediator( MediatorNames.WATCH_MEDIATOR, watchPage);
                
		this.facade.registerMediator( watchPageMediator );
		}
		vehicleproxy.getreminderbyid();
	} ,	
	registershare:function(index)
	{
	  
		currentid=vehicle[index].vehicle_id;
		currentemail=vehicle[index].e_mail;
		currentname=vehicle[index].first_name;
		$(jQuery("#useanother")).attr('checked',false);
		$(jQuery("#cardetcheck")).attr('checked',false);
		$(jQuery("#sellerdetcheck")).attr('checked',false);
		$(jQuery("#privatnotcheck")).attr('checked',false);
		$(jQuery("#towdetcheck")).attr('checked',false);
		$(jQuery("#mapcheck")).attr('checked',false);
		$("#selectdiv").show();
		$("#anoemaildiv").hide();
		$("#anoemail").val('');
		$("#anynote").val('');
		if(this.facade.hasMediator(MediatorNames.SHARE_MEDIATOR)==false)
		{
		var sharePage = new SharePage();
             
		var sharePageMediator = new SharePageMediator( MediatorNames.SHARE_MEDIATOR, sharePage);
                
		this.facade.registerMediator( sharePageMediator );
		}
		vehicleproxy.getPartnerlist();
	} , 
	registerprivatenote:function(index)
	{
	
		currentid=vehicle[index].vehicle_id;
		currentemail=vehicle[index].e_mail;
		vehicleproxy.getprivatenotes();
		if(this.facade.hasMediator(MediatorNames.PNOTE_POP_MEDIATOR)==false)
		{								
			var pnotepopPage = new PrivateNotePage();   
			var pnotepopMediator = new PrivateNoteMediator( MediatorNames.PNOTE_POP_MEDIATOR, pnotepopPage);
			this.facade.registerMediator( pnotepopMediator );
			
		}
	},	
   setTowStatus: function(vehicle_id, status)
   {
        btn_tow_status=status;
        
        bootbox.confirm("Are you sure you want yo change the status?", "Cancel", "Ok", function(result) {
			 if(result==true)
			 {
			 	VProxy.setTowStatus_list(vehicle_id);
			 }
			 });
        
   },
   showInfoPage:function(index,vehicle_role)
   {
             
		if(global_user.admin_type=="Tower Administrator")
			 window.location="view_page.php?id="+index;
		else if(vehicle_role==8)
                          window.location="view_page.php?id="+index;
                    else
                        vehicleproxy.checkLocked(index);
   },
   setCount: function()
	{     
            
               if(global_user.tab_newvehicle!=0)
               {    
		document.getElementById("ns-count").innerHTML = vehicleCounts.nscount;
                document.getElementById("ns-m-count").innerHTML = vehicleCounts.nsMsgcount;
               }
               if(global_user.tab_bidaccepted!=0)
               {    
		document.getElementById("ba-count").innerHTML = vehicleCounts.bacount;
                document.getElementById("ba-m-count").innerHTML = vehicleCounts.baMsgcount;
               }
               if(global_user.tab_allvehicles!=0)
               {    
		document.getElementById("all-count").innerHTML = vehicleCounts.nipcount;
		document.getElementById("all-m-count").innerHTML = vehicleCounts.nipMsgcount;
               } 
	}	

});

